package algorithm;

import java.util.HashMap;

public class Deformation {
    public static boolean isDeformation(String str1, String str2) {
        if (str1 == null || str1.length() < 1 || str2 == null || str2.length() < 1) {
            return false;
        }
        char[] chas1 = str1.toCharArray();
        char[] chas2 = str2.toCharArray();
        HashMap<Character, Integer> map = new HashMap<>();
        for (char c : chas1) {
            if (map.containsKey(c)) {
                map.put(c, map.get(c) + 1);
            } else {
                map.put(c, 1);
            }
        }

        for (char c : chas2) {
            if (!map.containsKey(c)) {
                return false;
            }
            map.put(c, map.get(c) - 1);
            if (map.get(c) == 0) {
                map.remove(c);
            }
        }

        return map.isEmpty();
    }

    public static void main(String[] args) {
        System.out.println(isDeformation("111222", "222111"));
    }
}
